//
//  UIScrollView+Refresh.m
//  IAlliance
//
//  Created by lidp on 2017/2/22.
//  Copyright © 2017年 lidp. All rights reserved.
//

#import "UIScrollView+Refresh.h"
#import <objc/runtime.h>
//#import "EFCRefreshFooter.h"

@implementation UIScrollView (Refresh)

- (void)addHeaderRefreshWithTarget:(id)target action:(SEL)action{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = NO;
    [header setAutomaticallyChangeAlpha:YES];
    self.mj_header = header;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [header endRefreshing];
    });
}

- (void)addFooterRefreshWithTarget:(id)target action:(SEL)action{
    if (!self.mj_footer) {
        MJRefreshBackStateFooter *footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:target refreshingAction:action];
        [footer setAutomaticallyChangeAlpha:YES];
        self.mj_footer = footer;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [footer endRefreshing];
        });
    }
}

- (void)endRefresh{
    [self removeContentView];
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

- (void)addPromptView:(NSString *)imageName title:(NSString *)title desc:(NSString *)desc{
    [self loadContentView];
    [self addSubview:self.contentView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [self.contentView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(0);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = DPColorForHex(@"#3d8eff");
    titleLabel.numberOfLines = 1;
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(imageView.mas_bottom).offset(20);
    }];
    
    UILabel *descLabel = nil;
    if (desc) {
        descLabel = [UILabel new];
        descLabel.text = desc;
        descLabel.font = [UIFont systemFontOfSize:12];
        descLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        descLabel.numberOfLines = 1;
        [self.contentView addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(12);
        }];
    }
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        CGFloat offset = 0;
        if ([self isKindOfClass:[UITableView class]]) {
            UIView *headerView = [(UITableView *)self tableHeaderView];
            if (headerView) {
                offset = headerView.viewHeight / 2;
            }
        }
        make.centerY.equalTo(self).offset(offset);
        make.width.equalTo(self);
        make.bottom.equalTo((descLabel ? descLabel : titleLabel));
    }];
}

- (void)removePromptView{
    [self removeContentView];
}

- (void)showLoadingView{
    [self showLoadingViewWithOffsetY:0];
}

- (void)showLoadingViewWithOffsetY:(CGFloat)offsetY{
    [self removeContentView];
    
    self.scrollEnabled = NO;
    [self loadContentView];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 50)];
    self.contentView.backgroundColor = [UIColor clearColor];
    self.contentView.center = CGPointMake(self.viewWidth / 2, self.viewHeight / 2 + offsetY);
    [self addSubview:self.contentView];
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicatorView startAnimating];
    [self.contentView addSubview:indicatorView];
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.equalTo(self.contentView);
    }];
    
    UILabel *stateLabel = [UILabel new];
    stateLabel.text = @"正在加载...";
    stateLabel.font = [UIFont systemFontOfSize:14];
    stateLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    stateLabel.numberOfLines = 1;
    [self.contentView addSubview:stateLabel];
    [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.equalTo(self.contentView);
    }];
}

- (void)showLoadFailedViewWithAction:(SEL)action target:(id)target{
    [self removeContentView];
    self.scrollEnabled = NO;
    [self loadContentView];
    [self addSubview:self.contentView];
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        CGFloat offset = 0;
        if ([self isKindOfClass:[UITableView class]]) {
            UIView *headerView = [(UITableView *)self tableHeaderView];
            if (headerView) {
                offset = headerView.viewHeight / 2;
            }
        }
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(offset);
        make.width.equalTo(self);
        make.height.mas_equalTo(172);
    }];
    
    UIImageView *tipsImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_load_failed"]];
    [self.contentView addSubview:tipsImageView];
    [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(0);
    }];
    
    UILabel *tipsLabel = [UILabel new];
    tipsLabel.text = @"网络异常 加载失败!";
    tipsLabel.font = [UIFont systemFontOfSize:16];
    tipsLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    tipsLabel.numberOfLines = 1;
    [self.contentView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(tipsImageView.mas_bottom).offset(8);
    }];
    
    UIButton *reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reloadBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [reloadBtn setTitle:@"重新加载" forState:UIControlStateNormal];
    [reloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    reloadBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    reloadBtn.backgroundColor = DPColorForHex(@"#3d8eff");
    reloadBtn.setCornerRadius(5);
    [self.contentView addSubview:reloadBtn];
    [reloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(120, 37));
    }];
}

- (void)showEmptyDataTipsViewWithTitle:(NSString *)title imageName:(NSString *)imageName{
    [self removeContentView];
//    self.scrollEnabled = NO;
    [self loadContentView];
    [self addSubview:self.contentView];
    
    if (imageName == nil || imageName.length == 0) {
        imageName = @"moban_wu";
    }
    
    UIImageView *tipsImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [self.contentView addSubview:tipsImageView];
    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *tipsLabel = [UILabel new];
    tipsLabel.text = title;
    tipsLabel.font = [UIFont systemFontOfSize:16];
    tipsLabel.textColor = DPColorForHex(@"#959595").setAlpha(0.64);
    tipsLabel.numberOfLines = 1;
    [self.contentView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(tipsImageView.mas_bottom).offset(13);
    }];
    
    [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.mas_equalTo(0);
    }];
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        CGFloat offset = 0;
        if ([self isKindOfClass:[UITableView class]]) {
            UIView *headerView = [(UITableView *)self tableHeaderView];
            if (headerView) {
                offset = headerView.viewHeight / 2;
            }else{
                offset = -50;
            }
        }
        if ([self isKindOfClass:[UICollectionView class]]) {
            offset = -50;
        }
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(offset);
        make.width.equalTo(self);
        make.bottom.equalTo(tipsLabel);
    }];
}

#pragma mark - ---------------------- contentView
- (UIView *)contentView{
    return objc_getAssociatedObject(self, @selector(contentView));
}

- (void)setContentView:(UIView *)contentView{
    return objc_setAssociatedObject(self, @selector(contentView), contentView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)loadContentView{
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor clearColor];
}

- (void)removeContentView{
    [self.contentView removeFromSuperview];
    self.contentView = nil;
    self.scrollEnabled = YES;
}

@end
